package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Result;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     */
    @Override
    public void add(DishDTO dishDTO) {
        //向菜品表插入数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.insert(dish);
        //获取insert生成的主键值
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishflavor -> dishflavor.setDishId(dishId));
        if (flavors != null && flavors.size() > 0) {
            //向口味表插入数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult search(DishPageQueryDTO dishPageQueryDTO) {
        //查询菜单中相关信息
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishPage = dishMapper.select(dishPageQueryDTO);
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            //判断当前菜品是否能够被删除->是否正在起售中？？
            Dish dish = dishMapper.getById(id);
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealDishIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            //删除菜品表中的菜品数据
            dishMapper.deleteById(id);
            //删除菜品关联的口味数据
            dishFlavorMapper.deleteById(id);
        }
    }

    /**
     * 菜品起售、停售
     *
     * @param status
     */
    @Override
    public void updateSalesStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateStatus(dish);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getInfoDish(Long id) {
        //根据id查询菜单中的数据
        Dish dish = dishMapper.getById(id);
        //根据id查询菜单id对应的口味信息
        List<DishFlavor> flavorList = dishFlavorMapper.getById(id);
        //封装成VO返回数据
        DishVO dishVO = DishVO.builder().flavors(flavorList).build();
        BeanUtils.copyProperties(dish, dishVO);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    public void updateDish(DishDTO dishDTO) {
        // 拷贝Dish对象数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 更新Dish 数据
        dishMapper.updateDish(dish);
        // 获取 当前菜品ID
        Long dishId = dish.getId();
        // 根据id 查询菜品对应的口味数据 -- 删除原来数据  重新添加新数据
        // 首先根据菜品id删除 对应的菜品口味
        dishFlavorMapper.deleteById(dishId);
        // 添加菜品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断口味是否为空
        if (flavors.size() > 0 && flavors != null) {
            flavors.forEach(dishflavor -> dishflavor.setDishId(dishId));
            dishFlavorMapper.insertBatch(flavors);
        }

    }
}
